<template>
  <page-header-wrapper :title="false">
    <a-card
      title="消息管理"
    >
      <template slot="extra">
        <a-button icon="plus" type="danger" @click="toAdd">发布消息</a-button>
      </template>
      <div class="table-operator">
        <s-table
          bordered
          ref="table"
          size="middle"
          :row-key="(record) => record.id"
          :columns="msgColumns"
          :data="messageData"
        >
          <template slot="msg_type" slot-scope="text">
            <a-tag color="red">
              {{msgOpts[text]}}
            </a-tag>
          </template>
          <template slot="action" slot-scope="text,record">
            <a-button size="small" type="primary" @click="toDetail(record.id)">详情</a-button>
            <a-button size="small" type="danger" @click="delMsg(record.id)">删除</a-button>
          </template>
        </s-table>
      </div>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { STable } from '@/components'
import col from '@/columns/message'
import { delMsg, getMessageList } from '@/api/message'

export default {
  components: {
    STable
  },
  name: 'AllMessage',
  data () {
    return {
      msgColumns: col.message,
      messageData: parameters => {
        return getMessageList(Object.assign(parameters, this.queryParam)).then(res => {
          this.msgOpts = res.result.msg_type
          return res.result
        })
      },
      msgOpts: {},
      queryParam: {}
    }
  },
  activated () {
    this.$refs.table.refresh(true)
  },
  methods: {
    toDetail (id) {
      this.$router.push({ name: 'MessageEdit', params: { id } })
    },
    delMsg (id) {
      const data = {}
      const arr = []
      arr[0] = id
      data['ids'] = arr
      delMsg(data).then(res => {
        this.$refs.table.refresh(true)
      })
    },
    toAdd () {
      this.$router.push({ name: 'MessageAdd' })
    }
  }
}
</script>

<style scoped>
.message-type {
  margin-bottom: 20px;
}
.message-type button {
  margin-right: 5px;
}
</style>
